package org.jeecg.modules.renlmd.job;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.bzBaseinfo.entity.HqBzBaseinfo;
import org.jeecg.modules.kuawcsFs.entity.AgKuawcsFs;
import org.jeecg.modules.kuawcsFs.service.IAgKuawcsFsService;
import org.jeecg.modules.renlmd.entity.HqStRenlmd;
import org.jeecg.modules.renlmd.service.IHqStRenlmdService;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.client.RestTemplate;

import java.io.*;
import java.time.*;
import java.util.*;


@Slf4j
public class syncRenlmd implements Job {
    @Autowired
    private IHqStRenlmdService hqStRenlmdService;

    @Autowired
    private IAgKuawcsFsService agKuawcsFsService;

    @Autowired
    RestTemplate restTemplate;

    @Value("${kwakcs.savePath}")
    String savePath ;

    @SneakyThrows
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        System.out.println((" 同步人流密度记录，任务执行了，时间:" + DateUtils.getTimestamp()));
        syncRenlmd();

    }

    void syncRenlmd(){

//读取数据
        List <HqStRenlmd> hqStRenlmd = hqStRenlmdService.list(new QueryWrapper <HqStRenlmd>().eq("chuanswczt","0"));

        if (!hqStRenlmd.isEmpty()){

            JSONArray jsonArray = new JSONArray();

            for (int i = 0; i < hqStRenlmd.size(); i++) {
                jsonArray.add(hqStRenlmd.get(i));
            }

            //写为文件
            BufferedWriter writer = null;
            //		String savePath="D:/Json测试";
            String fileName="hq_st_renlmd"+".json";
            String path =savePath+"/数据同步/"+fileName;
            File file = new File(path);
            String parentPath = file.getParent();
            File parentFile = new File(parentPath);
            if(!parentFile.exists()) {
                parentFile.mkdirs();
            }
            //如果文件不存在，则新建一个
            if(!file.exists()){
                try {
                    file.createNewFile();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            // 格式化json字符串
            String jsonData= JSON.toJSONString(jsonArray);
            //写入
            try {
                writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file,false), "UTF-8"));
                writer.write(jsonData);
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    if(writer != null){
                        writer.close();
                        System.out.println("文件生成成功！");

                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    System.out.println("文件生成失败！");
                }
            }

            hqStRenlmd.forEach(item -> {
                item.setChuanswczt("1");
                hqStRenlmdService.updateById(item);

            });


            //调用跨网传输
            AgKuawcsFs agKuawcsFs=new AgKuawcsFs();
            agKuawcsFs.setFasmbbm("houqin_erweima");
            agKuawcsFs.setFaswjm(fileName);
            agKuawcsFs.setFaswjlj(path);
            agKuawcsFs.setChuansfs("2");
            agKuawcsFs.setFassj(new Date());
            //  agKuawcsFsService.save(agKuawcsFs);
            agKuawcsFsService.sendFile(agKuawcsFs);
        }

    }

}
